package com.wenshi.web.controller.device;

import com.wenshi.common.core.domain.AjaxResult;
import com.wenshi.system.domain.Alarm;
import com.wenshi.system.service.ISysAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wenshi.web.socket.WebSocket.dealMap;

@RestController
@RequestMapping("/alarm")
public class SysDeviceAlarmController {

    @Autowired
    private ISysAlarmService alarmService;
    @GetMapping("/get")
    public AjaxResult getAlarmList(String deviceIp){
        List<Alarm> byDeviceIpList=alarmService.getByDeviceIpList(deviceIp);
        return AjaxResult.success(byDeviceIpList);
    }
    @GetMapping("/get/new")
    public AjaxResult getNewAlarm(String deviceIp){
        List<Alarm> alarmList=  alarmService.getNewAlarm(deviceIp);
        return  AjaxResult.success(alarmList);
    }
    @PostMapping("/clear")
    public AjaxResult clearAlarm(@RequestBody Alarm alarm){
        boolean status= alarmService.clear(alarm);

        if (status){
            dealMap.remove(alarm);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @GetMapping("/reset")
    @CrossOrigin
    public AjaxResult resetAllAlarm(String deviceIp){
        alarmService.resetAllAlarm(deviceIp);
        return AjaxResult.success();
    }

    @GetMapping("/clearAll")
    @CrossOrigin
    public AjaxResult clearAllAlarm(String deviceIp){
        boolean result = alarmService.clearAllAlarm(deviceIp);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
